﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using mmoss.orm;

public partial class CRMList : System.Web.UI.Page
{
    private User CurrentUser
    {
        get
        {
            return ((User)Session["crmUser"]);
        }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["crmUser"] == null) { Response.Redirect("login.aspx?msg=user timed out");
        return;
        }
        gvPatientList.RowCommand += new GridViewCommandEventHandler(gvPatientList_RowCommand);
        if (!Page.IsPostBack)
        {
            lblUserInfo.Text = CurrentUser.userName;
            loadGrid();
        }
    }
    void gvPatientList_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        //call comand executed
        if (e.CommandName == "Call")
        {
            int iRowIndex = int.Parse(e.CommandArgument.ToString());
            int patientID = int.Parse(((Label)gvPatientList.Rows[iRowIndex].FindControl("lblPatientID")).Text);
            Response.Redirect("CRMPatientEdit.aspx?listID="+patientID.ToString());
        }
    }
    private void loadGrid()
    {

        gvPatientList.DataSource = dbConn.db.CRMListPatients.Where
            (p => (p.Patient.patientPhoneHome != "" || p.Patient.patientPhoneCell != "") && p.Closed==false).OrderBy(p => p.Patient.patientDatePrescriptionExpiration).ToList();
        gvPatientList.DataBind();
    }
}